//
//  Copyright (c) 2020 Open Whisper Systems. All rights reserved.
//

#import "BaseModel.h"
#import <Mantle/MTLJSONAdapter.h>

NS_ASSUME_NONNULL_BEGIN

extern uint32_t const OWSDevicePrimaryDeviceId;

@class SDSAnyReadTransaction;
@class SDSAnyWriteTransaction;
@class SDSKeyValueStore;

@interface OWSDeviceManager : NSObject

+ (SDSKeyValueStore *)keyValueStore;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;

+ (instancetype)shared;

- (BOOL)mayHaveLinkedDevicesWithTransaction:(SDSAnyReadTransaction *)transaction;
- (void)setMayHaveLinkedDevices;
- (void)clearMayHaveLinkedDevices;

- (BOOL)hasReceivedSyncMessageInLastSeconds:(NSTimeInterval)intervalSeconds;
- (void)setHasReceivedSyncMessage;

@end

#pragma mark -

@interface OWSDevice : BaseModel <MTLJSONSerializing>

@property (nonatomic, readonly) NSInteger deviceId;
@property (nonatomic, readonly, nullable) NSString *name;
@property (nonatomic, readonly) NSDate *createdAt;
@property (nonatomic, readonly) NSDate *lastSeenAt;

+ (instancetype)new NS_UNAVAILABLE;
- (instancetype)init NS_UNAVAILABLE;
- (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithUniqueId:(NSString *)uniqueId NS_UNAVAILABLE;
- (instancetype)initWithGrdbId:(int64_t)grdbId uniqueId:(NSString *)uniqueId NS_UNAVAILABLE;

- (instancetype)initWithUniqueId:(NSString *)uniqueId
                       createdAt:(NSDate *)createdAt
                        deviceId:(NSInteger)deviceId
                      lastSeenAt:(NSDate *)lastSeenAt
                            name:(nullable NSString *)name
NS_DESIGNATED_INITIALIZER NS_SWIFT_NAME(init(uniqueId:createdAt:deviceId:lastSeenAt:name:));

// --- CODE GENERATION MARKER

// This snippet is generated by /Scripts/sds_codegen/sds_generate.py. Do not manually edit it, instead run `sds_codegen.sh`.

// clang-format off

- (instancetype)initWithGrdbId:(int64_t)grdbId
                      uniqueId:(NSString *)uniqueId
                       createdAt:(NSDate *)createdAt
                        deviceId:(NSInteger)deviceId
                      lastSeenAt:(NSDate *)lastSeenAt
                            name:(nullable NSString *)name
NS_DESIGNATED_INITIALIZER NS_SWIFT_NAME(init(grdbId:uniqueId:createdAt:deviceId:lastSeenAt:name:));

// clang-format on

// --- CODE GENERATION MARKER

+ (nullable instancetype)deviceFromJSONDictionary:(NSDictionary *)deviceAttributes error:(NSError **)error;

/**
 * The id of the device currently running this application
 */
+ (uint32_t)currentDeviceId;

/**
 *
 * @param transaction yapTransaction
 * @return
 *   If the user has any linked devices (apart from the device this app is running on).
 */
+ (BOOL)hasSecondaryDevicesWithTransaction:(SDSAnyReadTransaction *)transaction;

- (NSString *)displayName;
- (BOOL)isPrimaryDevice;

/**
 * Assign attributes to this device from another.
 *
 * @param other
 *  OWSDevice whose attributes to copy to this device
 * @return
 *  YES if any values on self changed, else NO
 */
- (BOOL)updateAttributesWithDevice:(OWSDevice *)other;

- (BOOL)areAttributesEqual:(OWSDevice *)other;

@end

NS_ASSUME_NONNULL_END
